//
//  TalkViewController.m
//  SeaChat
//
//  Created by 刘汉 on 16/6/16.
//  Copyright © 2016年 潘强. All rights reserved.
//

#import "TalkViewController.h"
#import "Login&RegViewController.h"
@interface TalkViewController ()<EMContactManagerDelegate,EMClientDelegate>

@end

@implementation TalkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//监听网络的状态
//设置代理
    [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
    [[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
   self.title = @"会话";

}
- (void)dealloc
{
    [[EMClient sharedClient]removeDelegate:self];
    [[EMClient sharedClient].contactManager removeDelegate:self];
    [super dealloc];

}
//监听自动连接的状态

#pragma mark -chatmanger 代理方法
- (void)didConnectionStateChanged:(EMConnectionState)aConnectionState{
    if (aConnectionState == EMConnectionDisconnected) {
        NSLog(@"网络断开,未连接");
        self.title = @"未连接";
    }else  {
        self.title = @"会话";

        NSLog(@"网络通了,连接成功");
    }
   }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 好友添加结果的代理方法
- (void)didReceiveAgreedFromUsername:(NSString *)aUsername{

    NSString *message = [NSString stringWithFormat:@"%@同意了你的好友添加请求",aUsername];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"好的" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [self presentViewController:alertController animated:1 completion:^{


    }];



}
- (void)didReceiveDeclinedFromUsername:(NSString *)aUsername{

    NSString *message = [NSString stringWithFormat:@"%@拒绝了你的好友添加请求",aUsername];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"好的" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

    }]];
    [self presentViewController:alertController animated:1 completion:^{
        
        
    }];
}
- (void)didReceiveFriendInvitationFromUsername:(NSString *)aUsername
                                       message:(NSString *)aMessage{



    NSString *message = [NSString stringWithFormat:@"%@请求加你为好友",aUsername];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:message message:aMessage preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"好的" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient].contactManager asyncAcceptInvitationForUsername:aUsername success:^{


        } failure:^(EMError *aError) {
            NSLog(@"%@",aError);

        }];



    }]];

    [alertController addAction:[UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient].contactManager declineInvitationForUsername:aUsername];

  [[EMClient sharedClient].contactManager asyncDeclineInvitationForUsername:aUsername success:^{


} failure:^(EMError *aError) {


}];




    }]];
    [self presentViewController:alertController animated:1 completion:^{


    }];



}



- (void)didLoginFromOtherDevice{
    Login_RegViewController *LRVC = [Login_RegViewController new];
    [self presentViewController:LRVC animated:1 completion:^{


    }];
    [LRVC release];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 0;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
